﻿package frEngine.primitives
{
    import __AS3__.vec.*;
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.core.*;
    import frEngine.event.*;
    import frEngine.render.*;
    import frEngine.shader.*;
    import frEngine.shader.filters.*;
    import frEngine.shader.filters.fragmentFilters.*;
    import frEngine.shader.filters.vertexFilters.*;
    import frEngine.shader.filters.vertexFilters.def.*;

    public class FrQuad extends Mesh3D
    {
        private var _x:Number;
        private var _y:Number;
        private var _width:Number;
        private var _height:Number;
        private var _fullScreenMode:Boolean = false;
        protected var _toChange:Boolean = false;
        protected var _sceneRect:Rectangle;
        protected var _bufferRect:Rectangle;
        private var _useBufferRect:Boolean = false;
        private var _Zdepth1:Number = 0;
        private var _Zdepth2:Number = 0;
        protected var _screenTransform:Matrix3D;
        protected var _vertexFileter:VertexFilter;
        private static var _surf:FrSurface3D;
        private static const defaultSceneRect:Rectangle = new Rectangle(0, 0, 800, 600);
        static const _temp0:Vector3D = new Vector3D();

        public function FrQuad(param1:String = "quad", param2:Number = 0, param3:Number = 0, param4:Number = 100, param5:Number = 100, param6:Boolean = false, param7 = null, param8:RenderList = null)
        {
            this._sceneRect = defaultSceneRect;
            this._screenTransform = new Matrix3D();
            super(param1, true, param8);
            this._vertexFileter = new TransformFilter(false, ECalculateOpType.WorldViewProj);
            this.setMaterial(param7, Texture3D.MIP_NONE, param7);
            this.addSurface(surf);
            this.setTo(param2, param3, param4, param5, param6);
            this.render = FrQuadRender.instance;
            return;
        }// end function

        override public function setShaderBase(param1:VertexFilter, param2:FragmentFilter) : void
        {
            super.setShaderBase(this._vertexFileter, param2);
            return;
        }// end function

        public function setZdepth(param1:Number, param2:Number) : void
        {
            if (this._Zdepth1 == param1 || this._Zdepth2 == param2)
            {
                return;
            }
            this._Zdepth1 = param1;
            this._Zdepth2 = param2;
            var _loc_3:* = _surf.getVertexBufferByNameId(FilterName_ID.POSITION_ID);
            var _loc_4:* = _loc_3.vertexVector;
            var _loc_5:* = this._Zdepth1;
            _loc_4[7] = this._Zdepth1;
            _loc_4[2] = _loc_5;
            var _loc_5:* = this._Zdepth2;
            _loc_4[17] = this._Zdepth2;
            _loc_4[12] = _loc_5;
            _loc_3.toUpdate();
            return;
        }// end function

        public function get Zdepth1() : Number
        {
            return this._Zdepth1;
        }// end function

        public function get Zdepth2() : Number
        {
            return this._Zdepth2;
        }// end function

        override public function disposeSurfaces() : void
        {
            this.surfaces = new Vector.<FrSurface3D>;
            return;
        }// end function

        public function set sceneRect(param1:Rectangle) : void
        {
            if (this._sceneRect.size != param1.size || this._sceneRect.x != param1.x || this._sceneRect.y != param1.y)
            {
                this._toChange = true;
                this._sceneRect.x = param1.x;
                this._sceneRect.y = param1.y;
                this._sceneRect.width = param1.width;
                this._sceneRect.height = param1.height;
            }
            return;
        }// end function

        public function UseBufferRect(param1:Boolean, param2:Rectangle) : void
        {
            if (param1 == this._useBufferRect)
            {
                if (this._useBufferRect == false || param2.equals(this._bufferRect))
                {
                    return;
                }
            }
            this._useBufferRect = param1;
            if (param2)
            {
                this._bufferRect = new Rectangle(param2.x, param2.y, param2.width, param2.height);
            }
            this._toChange = true;
            return;
        }// end function

        public function get sceneRect() : Rectangle
        {
            return this._sceneRect;
        }// end function

        override public function addedToScene(param1:Scene3D) : Boolean
        {
            if (this._scene == param1)
            {
                return true;
            }
            var _loc_2:* = super.addedToScene(param1);
            if (param1.viewPort)
            {
                this.sceneRect = param1.viewPort;
            }
            return _loc_2;
        }// end function

        override public function removedFromScene() : void
        {
            super.removedFromScene();
            return;
        }// end function

        protected function changTransformHander(event:Event) : void
        {
            this._toChange = true;
            if (this._sceneRect == defaultSceneRect && _scene && _scene.viewPort)
            {
                this.sceneRect = _scene.viewPort;
            }
            return;
        }// end function

        public function change() : void
        {
            var _loc_1:Number = NaN;
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            var _loc_4:Number = NaN;
            var _loc_5:* = this.getPosition(false, _temp0);
            if (this._useBufferRect && this._bufferRect)
            {
                _loc_1 = (_loc_5.x + this._bufferRect.x) / 2048;
                _loc_2 = (_loc_5.y + this._bufferRect.y) / 1024;
                _loc_3 = this._bufferRect.width / 2048;
                _loc_4 = this._bufferRect.height / 1024;
            }
            else
            {
                _loc_1 = _loc_5.x / this._sceneRect.width;
                _loc_2 = _loc_5.y / this._sceneRect.height;
                _loc_3 = this._width / this._sceneRect.width;
                _loc_4 = this._height / this._sceneRect.height;
                if (this._fullScreenMode)
                {
                    _loc_3 = 1 - _loc_1;
                    _loc_4 = 1 - _loc_2;
                }
            }
            this._screenTransform.identity();
            this._screenTransform.appendScale(_loc_3 || 0.0001, _loc_4 || 0.0001, 1);
            this._screenTransform.appendTranslation(-1 + _loc_3 + _loc_1 * 2, 1 - _loc_4 - _loc_2 * 2, 0);
            this._toChange = false;
            return;
        }// end function

        override public function updateChildrenTransform(param1:int = 0) : void
        {
            super.updateChildrenTransform(param1);
            this._toChange = true;
            return;
        }// end function

        public function setTo(param1:Number, param2:Number, param3:Number, param4:Number, param5:Boolean = false) : void
        {
            this.x = param1;
            this.y = param2;
            this._width = param3;
            this._height = param4;
            this._fullScreenMode = param5;
            this._toChange = true;
            return;
        }// end function

        public function set fullScreenMode(param1:Boolean) : void
        {
            if (this._fullScreenMode == param1)
            {
                return;
            }
            this._fullScreenMode = param1;
            this._toChange = true;
            return;
        }// end function

        public function get fullScreenMode() : Boolean
        {
            return this._fullScreenMode;
        }// end function

        public function set width(param1:int) : void
        {
            if (this._width == param1)
            {
                return;
            }
            this._width = param1;
            this._toChange = true;
            return;
        }// end function

        public function get width() : int
        {
            return this._width;
        }// end function

        public function set height(param1:int) : void
        {
            if (this._height == param1)
            {
                return;
            }
            this._height = param1;
            this._toChange = true;
            return;
        }// end function

        public function get height() : int
        {
            return this._height;
        }// end function

        override public function set x(param1:Number) : void
        {
            super.x = param1;
            this._toChange = true;
            return;
        }// end function

        override public function set y(param1:Number) : void
        {
            super.y = param1;
            this._toChange = true;
            return;
        }// end function

        override public function draw(param1:Boolean = true, param2:ShaderBase = null) : void
        {
            if (!_scene || !visible)
            {
                return;
            }
            if (this._toChange)
            {
                this.change();
            }
            Device3D.worldViewProj.copyFrom(this._screenTransform);
            render.draw(this, this.material);
            return;
        }// end function

        public static function get surf() : FrSurface3D
        {
            var _loc_1:Vector.<Number> = null;
            if (!_surf)
            {
                _surf = new FrSurface3D("quad");
                _surf.addVertexData(FilterName_ID.POSITION_ID, 3, false, null);
                _surf.addVertexData(FilterName_ID.UV_ID, 2, false, null);
                _loc_1 = _surf.getVertexBufferByNameId(FilterName_ID.POSITION_ID).vertexVector;
                _loc_1.push(-1, 1, 0, 0, 0);
                _loc_1.push(1, 1, 0, 1, 0);
                _loc_1.push(-1, -1, 0, 0, 1);
                _loc_1.push(1, -1, 0, 1, 1);
                _surf.indexVector = new Vector.<uint>;
                _surf.indexVector.push(0, 1, 2, 3, 2, 1);
            }
            return _surf;
        }// end function

    }
}
